function newtrig = removeUnsteadyTrigger(trig)
% newtrig = removeUnsteadyTrigger(trig)
% removes unsteady trigger that do not jump from zero and back
% assumes maximum number of equal trigvalues in a segment as actual trigger
% see also: removeRepeatedTrigger

if nargin<2,
    nMinTrigSamp = 1;
end
trigOn = find(diff([0,trig>0])>0);
trigOff = find(diff([0,trig>0])<0);

if (trigOn(end)>trigOff(end)),
    trigOff=([trigOff length(trig)]);
end

newtrig = zeros(size(trig));

for tcount=1:length(trigOn),
    % assume maximum number of equal trigvalues as actual trigger
    curIdx = trigOn(tcount):trigOff(tcount)-1;
    trigSegment = trig(curIdx);
    trigVals = unique(trigSegment);
    nTrigVals = zeros(size(trigVals));
    for k=1:length(trigVals),
        nTrigVals(k)=sum(trigVals(k)==trigSegment);
    end
    [dummy,trigValPos]=max(nTrigVals);
    newtrig(curIdx)=trigVals(trigValPos);
end

